package com.starsoft.frame.base.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.starsoft.frame.base.bean.UserContext;
import com.starsoft.frame.base.service.PreferenceService;
import com.starsoft.frame.base.service.UserService;
import com.starsoft.frame.core.web.WebUtil;


@RestController
@RequestMapping(value = "/core/preference")
public class PreferenceController {

	@Autowired
	PreferenceService ps=null;
	
	@RequestMapping(value = "/get")
	public Map<String, String> get(HttpServletRequest request) {
		Map<String, String> result = new HashMap<>();
		String[] prefCodes = request.getParameter("prefCodes").split(",");
		for (String prefCode : prefCodes) {
			String prefValue = ps.getPreference(prefCode, UserService.getUserContext());
			result.put(prefCode, prefValue);
		}
		return result;
	}

	@RequestMapping(value = "/saveValue")
	public Map<String, Object> saveValue(HttpServletRequest request) {
		UserContext user = UserService.getUserContext();
		String prefCode = request.getParameter("prefCode");
		String prefValue = request.getParameter("prefValue");
		ps.saveValue(prefCode, prefValue, user);
		return WebUtil.sucesseModel(null);
	}
	
}
